#!/usr/bin/perl -w
#
#
use lib qw(../lib);
use strict;

use DBI;
use POSIX qw(strftime);
use MTT::Libs::Masks;


my $dbh = DBI->connect(
	'dbi:mysql:database=mtt',
	'root',
	'a1s3an'
) || die DBI->errstr;
$dbh->do('SET NAMES utf8');

sub table_src($);

my @trunks = @{ $dbh->selectall_arrayref('SELECT * FROM trunks') };
my @masks = @{ $dbh->selectall_arrayref('SELECT mask FROM masks') };

my @quartes = (1, 2, 3, 4);
my @rec_on_switch = (0..23);
my $ros;
foreach my $tname ( @ARGV ) {

	$dbh->do( table_src $tname );
	$dbh->do("DELETE FROM $tname");
	
	my $sql = 'INSERT INTO ' . $tname . ' values(DEFAULT, ' . join(', ', map {'?'} (1..53)) . ')';
	my $sth = $dbh->prepare($sql);

	my @row;
	foreach (1..500) {

		if ( $tname =~ /hour/i || @quartes == 4 ) {
			$row[0] = $trunks[ int(rand scalar @trunks) ]->[12] ;
			$row[1] = $trunks[ int(rand scalar @trunks) ]->[1];
			$row[6] = strftime('%Y-%m-%d %H:%M:%S', localtime( time + 42000 - int rand 150000 ));
		}

		$row[2] = $masks[ int(rand scalar @masks) ]->[0];
		$row[3] = $masks[ int(rand scalar @masks) ]->[0];
		$row[4] = $masks[ int(rand scalar @masks) ]->[0];
		$row[5] = $masks[ int(rand scalar @masks) ]->[0];
		
		unless ( $tname =~/15/ ) {
			$row[7] = 0;
		}
		else {
			$row[7] = $quartes[0]; # int( rand scalar(@quartes) ) ];
			shift @quartes;
			@quartes = (1, 2, 3, 4) if @quartes == 0;
		}

		for (1..45) {
			$row[ $_ + 7 ] = sprintf("%.2f", rand 100);
		}
		
		$sth->execute(@row);
	#	print join(', ', @row), "\n";
	}

	#open(F, '<:encoding(UTF8)', 'data_finish_report_data.sql') || die $!;
	#my @data = <F
}

sub table_src($) {
	my ($tname) = @_;

	return <<EOF;
		CREATE TABLE IF NOT EXISTS $tname (
			id int(11) NOT NULL AUTO_INCREMENT,
			switch varchar(10) DEFAULT NULL,
			trunk varchar(20) DEFAULT NULL,
			a_mask_in varchar(13) DEFAULT NULL,
			a_mask_out varchar(13) DEFAULT NULL,
			b_mask_in varchar(13) DEFAULT NULL,
			b_mask_out varchar(13) DEFAULT NULL,
			date datetime DEFAULT NULL,
			quarter tinyint(3) unsigned DEFAULT NULL,
			MATTEMPTBHCA_bi int(10) unsigned DEFAULT NULL,
			MATTEMPTBHCA_in int(10) unsigned DEFAULT NULL,
			MATTEMPTBHCA_out int(10) unsigned DEFAULT NULL,
			NANSW_bi int(10) unsigned DEFAULT NULL,
			NANSW_in int(10) unsigned DEFAULT NULL,
			NANSW_out int(10) unsigned DEFAULT NULL,
			NSEIZREQ_bi int(10) unsigned DEFAULT NULL,
			NSEIZREQ_in int(10) unsigned DEFAULT NULL,
			NSEIZREQ_out int(10) unsigned DEFAULT NULL,
			NCONVSEC_bi int(10) unsigned DEFAULT NULL,
			NCONVSEC_in int(10) unsigned DEFAULT NULL,
			NCONVSEC_out int(10) unsigned DEFAULT NULL,
			AVGNCONVSEC_bi int(10) unsigned DEFAULT NULL,
			AVGNCONVSEC_in int(10) unsigned DEFAULT NULL,
			AVGNCONVSEC_out int(10) unsigned DEFAULT NULL,
			PANSW_bi varchar(13) DEFAULT NULL,
			PANSW_in varchar(13) DEFAULT NULL,
			PANSW_out varchar(13) DEFAULT NULL,
			BHLLOAD_bi varchar(13) DEFAULT NULL,
			BHLLOAD_in varchar(13) DEFAULT NULL,
			BHLLOAD_out varchar(13) DEFAULT NULL,
			MLOAD_bi varchar(13) DEFAULT NULL,
			MLOAD_in varchar(13) DEFAULT NULL,
			MLOAD_out varchar(13) DEFAULT NULL,
			ERLANG_bi varchar(13) DEFAULT NULL,
			ERLANG_in varchar(13) DEFAULT NULL,
			ERLANG_out varchar(13) DEFAULT NULL,
			PCALCLOST_bi varchar(13) DEFAULT NULL,
			PCALCLOST_in varchar(13) DEFAULT NULL,
			PCALCLOST_out varchar(13) DEFAULT NULL,
			NDEVREQ_bi int(10) unsigned DEFAULT NULL,
			NDEVREQ_in int(10) unsigned DEFAULT NULL,
			NDEVREQ_out int(10) unsigned DEFAULT NULL,
			NDELDEV_bi int(10) unsigned DEFAULT NULL,
			NDELDEV_in int(10) unsigned DEFAULT NULL,
			NDELDEV_out int(10) unsigned DEFAULT NULL,
			PCHANNELUSAGE_bi varchar(13) DEFAULT NULL,
			PCHANNELUSAGE_in varchar(13) DEFAULT NULL,
			PCHANNELUSAGE_out varchar(13) DEFAULT NULL,
			NDEV_bi int(10) unsigned DEFAULT NULL,
			NDEV_in int(10) unsigned DEFAULT NULL,
			NDEV_out int(10) unsigned DEFAULT NULL,
			NBLOCKDEV_bi int(10) unsigned DEFAULT NULL,
			NBLOCKDEV_in int(10) unsigned DEFAULT NULL,
			NBLOCKDEV_out int(10) unsigned DEFAULT NULL,
			KEY date_key (date),
			KEY switch (switch),
			KEY trunk (trunk(15)),
			KEY b_mask_in (b_mask_in),
			KEY b_mask_out (b_mask_out),
			KEY a_mask_in (a_mask_in),
			KEY a_mask_out (a_mask_out),
			KEY id (id)
			) ENGINE=MyISAM AUTO_INCREMENT=5001 DEFAULT CHARSET=utf8
EOF
;
}
